package org.example.innovate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.innovate.common.CommonResponse;
import org.example.innovate.domain.History;
import org.example.innovate.domain.User;
import org.example.innovate.service.HistoryService;
import org.example.innovate.mapper.HistoryMapper;
import org.example.innovate.util.JwtUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Administrator
* @description 针对表【history】的数据库操作Service实现
* @createDate 2025-03-17 14:55:40
*/
@Service
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History>
    implements HistoryService{

    @Resource
    private HistoryMapper historyMapper;

    @Override
    public CommonResponse<List<History>> getAll(HttpServletRequest request) {
        String token=request.getHeader("token");
        JwtUtil.verifyToken(token);
        User user= JwtUtil.getCurrentUser(token);
        if(user==null)
        {
            return CommonResponse.createForError("用户不存在");
        }
        List<History> historyList=historyMapper.selectLatestHistory(user.getUserId());
        return CommonResponse.createForSuccess("查询成功",historyList);
    }

    @Override
    public CommonResponse<List<History>> delete(HttpServletRequest request)
    {
        String token=request.getHeader("token");
        JwtUtil.verifyToken(token);
        User user=JwtUtil.getCurrentUser(token);
        if(user==null)
        {
            return CommonResponse.createForError("用户已存在");
        }
        QueryWrapper<History> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        List<History> historyList=historyMapper.selectList(queryWrapper);
        if(!historyList.isEmpty())
        {
            historyMapper.deleteBatchIds(historyList);
        }
        return CommonResponse.createForSuccess();
    }
}




